package cn.snowheart.ssm.service.impl;

import cn.snowheart.ssm.mapper.EmployeeMapper;
import cn.snowheart.ssm.pojo.Employee;
import cn.snowheart.ssm.pojo.EmployeeExample;
import cn.snowheart.ssm.pojo.EmployeeExample.Criteria;
import cn.snowheart.ssm.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author liuwanxiang
 * @version 2018/4/22
 */
@Component
public class EmployeeServiceImpl implements IEmployeeService {

    private static final int PAGE_SIZE = 5;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Override
    public List<Employee> list() {
        return employeeMapper.selectByExample(null);
    }

    @Override
    public PageInfo<Employee> getEmployeePage(Integer pageNo) {
        PageHelper.startPage(pageNo, PAGE_SIZE);
        List<Employee> employees = employeeMapper.selectByExampleWithDept(null);
        PageInfo<Employee> page = new PageInfo<>(employees, PAGE_SIZE);
        return page;
    }

    @Override
    public boolean checkUser(String empName) {
        EmployeeExample example = new EmployeeExample();
        Criteria criteria = example.createCriteria();
        criteria.andEmpNameEqualTo(empName);
        long count = employeeMapper.countByExample(example);
        return count == 0;
    }

    @Override
    public void saveEmployee(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    public Employee getEmployeeById(Integer id) {
        return employeeMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateEmployee(Employee employee) {
        employeeMapper.updateByPrimaryKeySelective(employee);
    }

    @Override
    public void deleteEmployee(String ids) {
        if (ids != null && ids.trim().length() > 0) {
            String[] idStrArray = ids.trim().split("-");
            for (String idStr : idStrArray) {
                employeeMapper.deleteByPrimaryKey(Integer.parseInt(idStr));
            }
        }
    }
}
